package com.bluetooth.phone;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.bluetooth.phone.multi.BaseViewHolder;
import com.bluetooth.phone.multi.TypeFactory;
import com.hsae.bluetooth.phones.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

public abstract class BaseAdapter extends RecyclerView.Adapter<BaseViewHolder<DockItem>> implements SwapClickListener {
    private static final String TAG = "BaseAdapter";
    protected boolean isShowDelete = false;//是否显示删除图标
    protected final List<DockItem> contentList = new ArrayList<>();
    protected static SwapClickListener mDialogSwapListener;
    protected static SwapClickListener mSubSwapListener;
    protected BaseViewHolder<DockItem> mDragViewHolder;
    private final HashMap<Integer, BaseViewHolder<DockItem>> mHolderList = new HashMap<>();

    protected DragListener mDragListener;

    public void setDialogSwapListener(SwapClickListener clickListener) {
        mDialogSwapListener = clickListener;
    }

    public void setSubSwapListener(SwapClickListener clickListener) {
        mSubSwapListener = clickListener;
    }

    protected TypeFactory mTypeFactory;

    public BaseAdapter(List<DockItem> dockItems) {
        if (dockItems.size() <= 0) {
            return;
        }
        contentList.clear();
        contentList.addAll(dockItems);
    }


//    @NonNull
//    @Override
//    public BaseViewHolder<DockItem> onCreateViewHolder(ViewGroup parent, int viewType) {
//        View view = LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false);
//        return mTypeFactory.createViewHolder(viewType, view);
//    }

    public TextView tv_content;
    public ImageView img_delete;//删除图标

    @NonNull
    @Override
    public BaseViewHolder<DockItem> onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false);
        return mTypeFactory.createViewHolder(viewType, view);
    }
    @Override
    public void onBindViewHolder(BaseViewHolder<DockItem> holder, int position) {
        final DockItem dockItem = contentList.get(position);

        holder.bindViewData(dockItem);
        holder.itemData = dockItem;
        holder.itemView.setTag(holder.getBindingAdapterPosition());
        tv_content = (TextView) holder.getView(R.id.tv_content);
        img_delete = (ImageView) holder.getView(R.id.img_delete);
        mHolderList.put(holder.getBindingAdapterPosition(), holder);
        if (isShowDelete) {
            img_delete.setVisibility(View.VISIBLE);
        } else {
            img_delete.setVisibility(View.GONE);
        }
        tv_content.setText(dockItem.getStr());
    }

    @Override
    public int getItemViewType(int position) {
       // return contentList.get(position).getItemType();
        Log.d(TAG, "getItemViewType  position==" + position);
        Log.d(TAG, "contentList.get(position).type(mTypeFactory) ==" + contentList.get(position).type(mTypeFactory));
          return contentList.get(position).type(mTypeFactory);
    }

    @Override
    public int getItemCount() {
        return  contentList.size();
    }

    public void startDrag(BaseViewHolder holder, BaseAdapter baseAdapter) {
        View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(holder.itemView);
        Object[] objects = new Object[]{holder, baseAdapter};
        holder.itemView.startDrag(null, shadowBuilder, objects,0 /*View.DRAG_FLAG_GLOBAL*/);
    }


    //直接添加在recycleView末尾

    /**
     * position == -1
     * 直接添加在recycleView末尾
     *
     * @param position item
     */
    protected void onDragItemEnter(int position) {

    }


    public void onItemDelete(int position, SwapClickListener swapClickListener) {
        Log.d(TAG, "onItemDelete  position==" + position);
        if (position == -1) {
            int dragPosition = mDragViewHolder.getBindingAdapterPosition();
            DockItem dockItem = contentList.get(dragPosition);
            contentList.remove(dragPosition); //移除数据
            notifyItemRemoved(dragPosition);//刷新数据移除
            notifyItemRangeChanged(dragPosition, contentList.size() - dragPosition);
            mDragViewHolder = null;
            swapClickListener.onItemEndInsert(dockItem);
            return;
        }

        if (mDragViewHolder != null) {
            int dragPosition = mDragViewHolder.getBindingAdapterPosition();
            if (dragPosition == -1){
                Log.e(TAG, "mDragViewHolder  dragPosition is out " );
                return;
            }
            DockItem dockItem = contentList.get(dragPosition);
            contentList.remove(dragPosition); //移除数据
            notifyItemRemoved(dragPosition);//刷新数据移除
            notifyItemRangeChanged(dragPosition, contentList.size() - dragPosition);
            mDragViewHolder = null;
            swapClickListener.onItemInsert(dockItem);
            return;
        }

        //点击
        DockItem dockItem = contentList.get(position);
        contentList.remove(position); //移除数据
        notifyItemRemoved(position);//刷新数据移除
        notifyItemRangeChanged(position, contentList.size() - position);//保留动画
        swapClickListener.onItemInsert(dockItem);

    }

    public void onDragItemEnd(int targetPosition, boolean hasEnterDrag) {
        if (hasEnterDrag) {
            Log.d(TAG, "onItemDelete  position==" + targetPosition);
            if (targetPosition == 0) {
                //已经放第最前面了，不处理
                return;
            }
            if (targetPosition == 1) {
                Log.e(TAG, "insert position 0");
                contentList.add(0, contentList.get(targetPosition));
                notifyItemInserted(0);
                notifyItemRangeChanged(0, contentList.size());
                return;
            }

            if (contentList.size() > 1) {
                Collections.swap(contentList, 1, 0);
                //刷新位置交换
                notifyItemMoved(1, 0);
            }
        }
    }

    public void onDragItemToMove(int toPosition) {
    }


    public void insert(BaseViewHolder source, int positionTo) {
        source.itemView.setAlpha(0);
        contentList.add(positionTo, contentList.get(source.getBindingAdapterPosition()));
        mSubSwapListener.onItemMoved(positionTo, positionTo+1);
    }

    public void moveDataItem(int fromPosition, int toPosition) {
        //考虑到跨position(如0->2)移动的时候处理不能Collections.swap
        // if(from<to) to = to + 1 - 1;//+1是因为add的时候应该是to位置后1位，-1是因为先remove了from所以位置往前挪了1位
    }

}